{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# 第一章~第二章：机器学习简介及线性回归...............学生：侯松林\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 进阶作业"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一、数据说明： Capital Bikeshare （美国Washington, D.C.的一个共享单车公司）提供的共享单车数据。数据包含每天的日期、天气等信息，需要预测每天的共享单车骑行量。数据探索，特征工程，模型训练三个可执行文件分别提交。\n",
    "1. 对数据做数据探索分析（可参考EDA_BikeSharing.ipynb，不计分）\n",
    "2. 适当的特征工程（可参考FE_BikeSharing.ipynb，不计分）\n",
    "3. 对全体数据，随机选择其中80%做训练数据，剩下20%为测试数据，评价指标为RMSE。（10分）\n",
    "4. 用训练数据训练最小二乘线性回归模型（20分）、岭回归模型、Lasso模型，其中岭回归模型（30分）和Lasso模型（30分），注意岭回归模型和Lasso模型的正则超参数调优。\n",
    "5. 比较用上述三种模型得到的各特征的系数，以及各模型在测试集上的性能。并简单说明原因。（10分）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>dteday</th>\n",
       "      <th>season</th>\n",
       "      <th>yr</th>\n",
       "      <th>mnth</th>\n",
       "      <th>holiday</th>\n",
       "      <th>weekday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>weathersit</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>casual</th>\n",
       "      <th>registered</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2011-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.344167</td>\n",
       "      <td>0.363625</td>\n",
       "      <td>0.805833</td>\n",
       "      <td>0.160446</td>\n",
       "      <td>331</td>\n",
       "      <td>654</td>\n",
       "      <td>985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>2011-01-02</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.363478</td>\n",
       "      <td>0.353739</td>\n",
       "      <td>0.696087</td>\n",
       "      <td>0.248539</td>\n",
       "      <td>131</td>\n",
       "      <td>670</td>\n",
       "      <td>801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>2011-01-03</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.196364</td>\n",
       "      <td>0.189405</td>\n",
       "      <td>0.437273</td>\n",
       "      <td>0.248309</td>\n",
       "      <td>120</td>\n",
       "      <td>1229</td>\n",
       "      <td>1349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>2011-01-04</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.212122</td>\n",
       "      <td>0.590435</td>\n",
       "      <td>0.160296</td>\n",
       "      <td>108</td>\n",
       "      <td>1454</td>\n",
       "      <td>1562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>2011-01-05</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.226957</td>\n",
       "      <td>0.229270</td>\n",
       "      <td>0.436957</td>\n",
       "      <td>0.186900</td>\n",
       "      <td>82</td>\n",
       "      <td>1518</td>\n",
       "      <td>1600</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   instant      dteday  season  yr  mnth  holiday  weekday  workingday  \\\n",
       "0        1  2011-01-01       1   0     1        0        6           0   \n",
       "1        2  2011-01-02       1   0     1        0        0           0   \n",
       "2        3  2011-01-03       1   0     1        0        1           1   \n",
       "3        4  2011-01-04       1   0     1        0        2           1   \n",
       "4        5  2011-01-05       1   0     1        0        3           1   \n",
       "\n",
       "   weathersit      temp     atemp       hum  windspeed  casual  registered  \\\n",
       "0           2  0.344167  0.363625  0.805833   0.160446     331         654   \n",
       "1           2  0.363478  0.353739  0.696087   0.248539     131         670   \n",
       "2           1  0.196364  0.189405  0.437273   0.248309     120        1229   \n",
       "3           1  0.200000  0.212122  0.590435   0.160296     108        1454   \n",
       "4           1  0.226957  0.229270  0.436957   0.186900      82        1518   \n",
       "\n",
       "    cnt  \n",
       "0   985  \n",
       "1   801  \n",
       "2  1349  \n",
       "3  1562  \n",
       "4  1600  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "df = pd.read_csv(\"day.csv\") #导入数据\n",
    "df.head() #查看数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>season_1</th>\n",
       "      <th>season_2</th>\n",
       "      <th>season_3</th>\n",
       "      <th>season_4</th>\n",
       "      <th>mnth_1</th>\n",
       "      <th>mnth_2</th>\n",
       "      <th>mnth_3</th>\n",
       "      <th>mnth_4</th>\n",
       "      <th>mnth_5</th>\n",
       "      <th>mnth_6</th>\n",
       "      <th>...</th>\n",
       "      <th>weekday_0</th>\n",
       "      <th>weekday_1</th>\n",
       "      <th>weekday_2</th>\n",
       "      <th>weekday_3</th>\n",
       "      <th>weekday_4</th>\n",
       "      <th>weekday_5</th>\n",
       "      <th>weekday_6</th>\n",
       "      <th>weathersit_1</th>\n",
       "      <th>weathersit_2</th>\n",
       "      <th>weathersit_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 26 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  mnth_4  \\\n",
       "0         1         0         0         0       1       0       0       0   \n",
       "1         1         0         0         0       1       0       0       0   \n",
       "2         1         0         0         0       1       0       0       0   \n",
       "3         1         0         0         0       1       0       0       0   \n",
       "4         1         0         0         0       1       0       0       0   \n",
       "\n",
       "   mnth_5  mnth_6  ...  weekday_0  weekday_1  weekday_2  weekday_3  weekday_4  \\\n",
       "0       0       0  ...          0          0          0          0          0   \n",
       "1       0       0  ...          1          0          0          0          0   \n",
       "2       0       0  ...          0          1          0          0          0   \n",
       "3       0       0  ...          0          0          1          0          0   \n",
       "4       0       0  ...          0          0          0          1          0   \n",
       "\n",
       "   weekday_5  weekday_6  weathersit_1  weathersit_2  weathersit_3  \n",
       "0          0          1             0             1             0  \n",
       "1          0          0             0             1             0  \n",
       "2          0          0             1             0             0  \n",
       "3          0          0             1             0             0  \n",
       "4          0          0             1             0             0  \n",
       "\n",
       "[5 rows x 26 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#对类别特征做独热编码\n",
    "categorical_features = ['season','mnth','weekday','weathersit']\n",
    "for col in categorical_features:\n",
    "    df[col] = df[col].astype(\"object\") \n",
    "x_cat = df[categorical_features]\n",
    "x_df_cat = pd.get_dummies(x_cat)\n",
    "x_df_cat.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.826662</td>\n",
       "      <td>-0.679946</td>\n",
       "      <td>1.250171</td>\n",
       "      <td>-0.387892</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.721095</td>\n",
       "      <td>-0.740652</td>\n",
       "      <td>0.479113</td>\n",
       "      <td>0.749602</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.634657</td>\n",
       "      <td>-1.749767</td>\n",
       "      <td>-1.339274</td>\n",
       "      <td>0.746632</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-1.614780</td>\n",
       "      <td>-1.610270</td>\n",
       "      <td>-0.263182</td>\n",
       "      <td>-0.389829</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1.467414</td>\n",
       "      <td>-1.504971</td>\n",
       "      <td>-1.341494</td>\n",
       "      <td>-0.046307</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       temp     atemp       hum  windspeed\n",
       "0 -0.826662 -0.679946  1.250171  -0.387892\n",
       "1 -0.721095 -0.740652  0.479113   0.749602\n",
       "2 -1.634657 -1.749767 -1.339274   0.746632\n",
       "3 -1.614780 -1.610270 -0.263182  -0.389829\n",
       "4 -1.467414 -1.504971 -1.341494  -0.046307"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "#对数值型数据去量纲\n",
    "numerical_features = ['temp', 'atemp', 'hum', 'windspeed']\n",
    "ss_x = StandardScaler()\n",
    "x_num = ss_x.fit_transform(df[numerical_features])\n",
    "x_df_num = pd.DataFrame(data=x_num, columns=numerical_features, index=df.index)\n",
    "x_df_num.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>season_1</th>\n",
       "      <th>season_2</th>\n",
       "      <th>season_3</th>\n",
       "      <th>season_4</th>\n",
       "      <th>mnth_1</th>\n",
       "      <th>mnth_2</th>\n",
       "      <th>mnth_3</th>\n",
       "      <th>mnth_4</th>\n",
       "      <th>mnth_5</th>\n",
       "      <th>mnth_6</th>\n",
       "      <th>...</th>\n",
       "      <th>weathersit_1</th>\n",
       "      <th>weathersit_2</th>\n",
       "      <th>weathersit_3</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>holiday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.826662</td>\n",
       "      <td>-0.679946</td>\n",
       "      <td>1.250171</td>\n",
       "      <td>-0.387892</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>-0.721095</td>\n",
       "      <td>-0.740652</td>\n",
       "      <td>0.479113</td>\n",
       "      <td>0.749602</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>-1.634657</td>\n",
       "      <td>-1.749767</td>\n",
       "      <td>-1.339274</td>\n",
       "      <td>0.746632</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>-1.614780</td>\n",
       "      <td>-1.610270</td>\n",
       "      <td>-0.263182</td>\n",
       "      <td>-0.389829</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>-1.467414</td>\n",
       "      <td>-1.504971</td>\n",
       "      <td>-1.341494</td>\n",
       "      <td>-0.046307</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1600</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 33 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  mnth_4  \\\n",
       "0         1         0         0         0       1       0       0       0   \n",
       "1         1         0         0         0       1       0       0       0   \n",
       "2         1         0         0         0       1       0       0       0   \n",
       "3         1         0         0         0       1       0       0       0   \n",
       "4         1         0         0         0       1       0       0       0   \n",
       "\n",
       "   mnth_5  mnth_6  ...  weathersit_1  weathersit_2  weathersit_3      temp  \\\n",
       "0       0       0  ...             0             1             0 -0.826662   \n",
       "1       0       0  ...             0             1             0 -0.721095   \n",
       "2       0       0  ...             1             0             0 -1.634657   \n",
       "3       0       0  ...             1             0             0 -1.614780   \n",
       "4       0       0  ...             1             0             0 -1.467414   \n",
       "\n",
       "      atemp       hum  windspeed  holiday  workingday   cnt  \n",
       "0 -0.679946  1.250171  -0.387892        0           0   985  \n",
       "1 -0.740652  0.479113   0.749602        0           0   801  \n",
       "2 -1.749767 -1.339274   0.746632        0           1  1349  \n",
       "3 -1.610270 -0.263182  -0.389829        0           1  1562  \n",
       "4 -1.504971 -1.341494  -0.046307        0           1  1600  \n",
       "\n",
       "[5 rows x 33 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把数据拼装起来\n",
    "mm_df = pd.concat([x_df_cat, x_df_num, df['holiday'], df['workingday'],df['cnt']], axis = 1, ignore_index=False)\n",
    "mm_df.to_csv('mm_day.csv',index=False) #将预处理后的数据进行保存\n",
    "mm_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 下面进行线性回归处理数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rmse_lr_test is  1322.785655787234\n",
      "rmse_lr_train is  1234.289371141277\n",
      "rmse_ridge_test is  1301.5677834681908\n",
      "rmse_ridge_train is  1240.5594457387972\n",
      "alpha of ridge is  10.0\n",
      "rmse_lasso_test is  1305.2656900542372\n",
      "rmse_lasso_train is  1239.8629441006376\n",
      "alpha of lasso is  8.497837508324915\n",
      "coef of lr is  [ 4.04255106e+16  4.04255106e+16  4.04255106e+16  4.04255106e+16\n",
      " -1.12125948e+15 -1.12125948e+15 -1.12125948e+15 -1.12125948e+15\n",
      " -1.12125948e+15 -1.12125948e+15 -1.12125948e+15 -1.12125948e+15\n",
      " -1.12125948e+15 -1.12125948e+15 -1.12125948e+15 -1.12125948e+15\n",
      " -2.33214785e+13 -2.24501784e+14 -2.24501784e+14 -2.24501784e+14\n",
      " -2.24501784e+14 -2.24501784e+14 -2.33214785e+13 -3.56455947e+14\n",
      " -3.56455947e+14 -3.56455947e+14  1.16000000e+03  8.80000000e+01\n",
      " -4.24000000e+02 -2.65000000e+02  2.01180306e+14  2.01180306e+14]\n",
      "coef of ridge is  [-567.42943846  214.65699242 -316.01988633  668.79233236 -125.43626149\n",
      " -168.78513143  185.56644194  -86.61533306   61.7397049   -93.06998113\n",
      " -598.05404183   27.31160266  624.8347286   392.77055247 -134.39957642\n",
      "  -85.8627052   -68.3786972  -171.84865529   26.22951984   70.43794014\n",
      "  -16.79115139   41.1883982   119.1626457   571.5181919   293.45696108\n",
      " -864.97515298  714.8003612   445.69671509 -416.47241157 -272.49507309\n",
      " -245.52451191  194.74056341]\n",
      "coef of lasso is  [-7.82845859e+02  3.35057347e+01 -4.72121033e+02  4.95673022e+02\n",
      " -0.00000000e+00 -0.00000000e+00  2.29546127e+02 -0.00000000e+00\n",
      "  7.64722378e+00 -0.00000000e+00 -6.25033886e+02  0.00000000e+00\n",
      "  6.54892911e+02  4.36196679e+02 -0.00000000e+00 -0.00000000e+00\n",
      " -8.62269106e+01 -1.61193711e+02  0.00000000e+00  1.42554827e+01\n",
      " -0.00000000e+00  0.00000000e+00  4.40715088e-01  2.78153022e+02\n",
      " -0.00000000e+00 -1.43332331e+03  1.04334379e+03  1.47479783e+02\n",
      " -3.94700713e+02 -2.64791960e+02 -1.71941065e+02  1.32800607e+02]\n"
     ]
    },
    {
     "data": {
      "image/png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NAWqA+nBtXQZcDlwelhvw2xD3MjrotZniuK6KOV8vAyenKK4PEzWPLY35v3VOJpyz5pdGGhARkZRQk5qIiKSEEo6IiKSEEo6IiKSEEo6IiKSEEo6IiKSEEo6IiKSEEo6IiKSEEo6IiKTE/wBee4LUtkC2pgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the best alpha of ridge is  10.0\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZAAAABfCAYAAADGdpEZAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAATQElEQVR4nO3de3BcV33A8e9vVytp9X7betiWkygYO44TIztOQqjHJq3D0DhtIM0DEvOYtAGGpKUDgWGgBIaBtkApBDKBAAlkcNwEiJuGcQN5AU1kZMdO/Ihj+S1Ltt5aPVfa3V//uFeyrEjWam1pH/p9Zu7s3XPOveecvdL97T33saKqGGOMMdPliXcDjDHGJCcLIMYYY2JiAcQYY0xMLIAYY4yJiQUQY4wxMbEAYowxJiZp8W7AbCkpKdHq6up4N8MYY5LKjh072lS1dKK8ORNAqqurqa+vj3czjDFmVrX0DFKWmxnz8iJybLI8G8IyxpgUdai1l3X//hK/eHXSGHBeLIAYY0wK6h8Kcc8vdpCe5mHdkrIZqWPODGEZY8xcoap8/ldvcLCll59/9CoqCvwzUo8dgRhjTIr5xavHeHpXE5+5/lLeXVMyY/VYADHGmBSyrynAA8/sY92SMj6x9pIZrcsCiDHGpIhQOMJnn9pNvj+db31wBR6PzGh9dg7EGGNSxMN/OMyekwF+eMdKCrPTZ7w+OwIxxpgUcKi1l//43UFuuGw+Nywv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qGVfUKoApYLSKXjSsy69tkLgWQRmDst/AqoGmyMiKSBuSTmMMSU/ZFVdtVNei+/RHwrllq24UWzXZLeKoaGBmCUNVnAZ+IlMS5WZMSER/OTvdxVf3VBEWSYrtM1Y9k2y4AqtoFvAhsGJc16/uvuRRA/gzUiMhiEUnHOcm0dVyZrcBd7vwHgOfVPSOVYKbsy7jx6Btxxn+T0VbgTveqnzVAt6o2x7tR0yUi80fGo0VkNc7/Xnt8WzUxt52PAPtV9duTFEv47RJNP5Jlu4hIqYgUuPN+4L3Am+OKzfr+a878HoiqhkTkU8A2nKuYfqKqe0XkAaBeVbfi/LH9XEQacCL3rfFr8eSi7MunReRGIITTl01xa/A5iMgvca6EKRGRRuDLOCcIUdWHgGdxrvhpAPqBj8SnpecWRT8+ANwjIiFgALg1Qb+cAFwLfBh4wx1zB/gCsBCSartE049k2S7lwKMi4sUJcltU9Zl477/sTnRjjDExmUtDWMYYYy4gCyDGGGNiYgHEGGNMTCyAGGOMiYkFEGOMMTGxAGKMMSYmFkCMMcbExAKIMcaYmPw/AX5YKmisNkQAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the best alpha of lasso is  8.497837508324915\n"
     ]
    }
   ],
   "source": [
    "#从预处理数据中取出特征X和输出y\n",
    "y=mm_df['cnt']\n",
    "X=mm_df.drop(['cnt'],axis=1)\n",
    "\n",
    "feat_names = X.columns\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=33,test_size=0.2)#将数据分成训练集和测试集两部分\n",
    "\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.linear_model import RidgeCV\n",
    "from sklearn.linear_model import LassoCV\n",
    "\n",
    "#最小二乘\n",
    "lr= LinearRegression()\n",
    "lr.fit(X_train,y_train) #训练\n",
    "y_test_pred_lr = lr.predict(X_test) #在测试集上预测\n",
    "y_train_pred_lr = lr.predict(X_train) #在训练集上预测\n",
    "\n",
    "#岭回归\n",
    "alphas = [0.01,0.1,1,10,100]\n",
    "ridge = RidgeCV(alphas = alphas, store_cv_values=True)#岭回归模型\n",
    "ridge.fit(X_train,y_train)\n",
    "y_test_pred_ridge = ridge.predict(X_test) #在测试集上预测\n",
    "y_train_pred_ridge = ridge.predict(X_train) #在训练集上预测\n",
    "\n",
    "#lasso\n",
    "lasso = LassoCV()#Lasso回归\n",
    "lasso.fit(X_train,y_train)\n",
    "y_test_pred_lasso = lasso.predict(X_test)#在测试集上预测\n",
    "y_train_pred_lasso = lasso.predict(X_train)#在训练集上预测\n",
    "\n",
    "#评估\n",
    "from sklearn import metrics\n",
    "\n",
    "rmse_lr_test = (metrics.mean_squared_error(y_test_pred_lr, y_test))**0.5\n",
    "rmse_lr_train = (metrics.mean_squared_error(y_train_pred_lr, y_train))**0.5\n",
    "\n",
    "rmse_ridge_test = (metrics.mean_squared_error(y_test_pred_ridge, y_test))**0.5\n",
    "rmse_ridge_train = (metrics.mean_squared_error(y_train_pred_ridge, y_train))**0.5\n",
    "\n",
    "rmse_lasso_test = (metrics.mean_squared_error(y_test_pred_lasso, y_test))**0.5\n",
    "rmse_lasso_train = (metrics.mean_squared_error(y_train_pred_lasso, y_train))**0.5\n",
    "\n",
    "print(\"rmse_lr_test is \", rmse_lr_test)\n",
    "print(\"rmse_lr_train is \", rmse_lr_train)\n",
    "print(\"rmse_ridge_test is \", rmse_ridge_test)\n",
    "print(\"rmse_ridge_train is \", rmse_ridge_train)\n",
    "print(\"alpha of ridge is \", ridge.alpha_)\n",
    "print(\"rmse_lasso_test is \", rmse_lasso_test)\n",
    "print(\"rmse_lasso_train is \", rmse_lasso_train)\n",
    "print(\"alpha of lasso is \", lasso.alpha_)\n",
    "print(\"coef of lr is \", lr.coef_)\n",
    "print(\"coef of ridge is \", ridge.coef_)\n",
    "print(\"coef of lasso is \", lasso.coef_)\n",
    "\n",
    "\n",
    "#最小二乘可视化，差值直方图\n",
    "plt.subplot(3, 1, 1)\n",
    "plt.hist(y_train-y_train_pred_lr,bins=40,color='b',alpha=0.5)\n",
    "\n",
    "#调优过程\n",
    "#岭回归\n",
    "mse_mean = np.mean(ridge.cv_values_,axis=0)\n",
    "plt.subplot(3, 1, 2)\n",
    "plt.plot(np.log10(alphas),mse_mean.reshape(len(alphas),1))\n",
    "plt.show()\n",
    "print('the best alpha of ridge is ',ridge.alpha_)\n",
    "#Lasso\n",
    "mse_mean = np.mean(lasso.mse_path_,axis=1)\n",
    "plt.subplot(3, 1, 3)\n",
    "plt.plot(np.log10(lasso.alphas_),mse_mean)\n",
    "plt.show()\n",
    "print('the best alpha of lasso is ',lasso.alpha_)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从上述结果的评价指标RMSE，在测试集上，3个模型由优到劣依次为：岭回归、Lasso、最小二乘， 而在训练集上，三者的优劣顺序为：最小二乘、LassoCV、岭回归。另外，我们可以看到Lasso训练得到的特征系数中，有一部分为0，这也符合Lasso的特点。 原因可以概况为：模型维度不够、数据量不够多。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:tf1.14]",
   "language": "python",
   "name": "conda-env-tf1.14-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
